<template>
  <a-modal
    :title="title"
    :width="460"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    okText="保存">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="版号名称"
          hasFeedback >
          <a-input placeholder="请输入版号名称" v-model="category_name"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="版号编号"
          hasFeedback >
          <a-input placeholder="请输入版号编号" v-model="category_code"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="选择品类"
          hasFeedback >
          <a-select v-model="select_pinglei" style="color: #827d7d" placeholder="请选择品类">
            <a-select-option :value="item.id" v-for="item in dingzhi_canshuList" >{{ item.categoryName }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="版号说明"
          hasFeedback >
          <a-input placeholder="请输入版号说明" v-model="select_topcategory"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设置排序"
          hasFeedback >
          <a-input placeholder="请输入版号排列序号" v-model="category_num"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="图片上传"
          hasFeedback >
          <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-image-upload class="avatar-uploader" text="上传" v-model="fileList" style="margin-top: 8px;"></j-image-upload>
          </a-form-item>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, postAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JImageUpload from '../../../components/jeecg/JImageUpload'
  import Qs from "qs";

  export default {
    name: "JeecgDemoModal",
    components: {
      JImageUpload
    },
    data () {
      return {
        category_name:'',
        category_code:'',
        category_num:'0',
        select_pinglei:'请选择品类',
        select_topcategory:'',
        fileList: [],
        categorydetail:[],
        dingzhi_canshuList: [],//定制下的参数列表
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          fileUpload: window._CONFIG['domianURL'] + "/sys/common/upload",
          query_allpinglei:"/admin/testplate/query_all_pinglei",
          query_detail:"/admin/parameter/query_testcategoryDetail",
          add: "/admin/testplate/pinglei_transfer_banhao",
          edit: "/admin/parameter/update_category_pinglei"
        },
      }
    },
    computed: {
      uploadAction: function() {
        return this.url.fileUpload;
      }
    },
    created () {
    },
    methods: {
      edit (record) {
        this.category_name='';
        this.category_code='';
        this.select_pinglei='请选择品类';
        this.select_topcategory='';
        this.category_num='0';
        this.fileList='';
        this.model = Object.assign({}, record);
        this.visible = true;//开启弹框
        if(record=="0"){
          this.title="添加版号"
          this.show_canshulist();
        }else {
          this.title="编辑版号"
          // this.show_categorydetail(this.model.id);
        }
      },
      show_canshulist() {
        let params = {};
        getAction(this.url.query_allpinglei,params).then((res)=>{
          if(res.success){
            this.dingzhi_canshuList = res.result;
          }else {
            this.$message.error("暂无品类数据");
          }
        });
      },
      // show_categorydetail(id) {
      //   let params = {id:id};
      //   getAction(this.url.query_detail,params).then((res)=>{
      //     console.info(res);
      //     if(res.success){
      //       this.categorydetail = res.result;
      //       this.category_name=this.categorydetail.categoryName;
      //       this.category_num=this.categorydetail.numbers;
      //       if(this.categorydetail.categorySex=='0'){
      //         this.select_sex='全部';
      //       }else if(this.categorydetail.categorySex=='1'){
      //         this.select_sex='男装';
      //       }else {
      //         this.select_sex='女装';
      //       }
      //       this.select_topcategory=this.categorydetail.remark;
      //       setTimeout(() => {
      //         this.fileList = this.categorydetail.img;
      //       }, 5)
      //
      //     }else {
      //       this.$message.error("查询失败");
      //     }
      //   });
      // },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if(this.category_name==null || this.category_name==''){
            this.$message.warning("请输入版号名称");
            return;
          }else if(this.category_code=='' || this.category_code==null){
            this.$message.warning("请输入版号编码");
            return;
          }
          else if(this.select_pinglei=='请选择品类'){
            this.$message.warning("请选择你要复制的品类");
            return;
          }else if(this.category_num=='' || this.category_num==null){
            this.$message.warning("请输入排序");
            return;
          }else if(this.fileList == null || this.fileList.length <= 0){
            this.$message.warning("请上传品类图片");
            return;
          }
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            let params = '';
            if(this.model.id!=null && this.model.id!=''){
              httpurl+=this.url.edit;
              method = 'put';
              params = {categoryName:this.category_name,sex:this.select_sex,remark:this.select_topcategory,
                numbers:this.category_num,img:this.fileList,id:this.model.id}
            }else{
              httpurl+=this.url.add;
              method = 'post';
              params = {banhao_name:this.category_name,banhao_code:this.category_code,banhao_remark:this.select_topcategory,
                category_id:this.select_pinglei,banhao_img:this.fileList,banhao_numbers:this.category_num}
            }
            httpAction(httpurl,params,method).then((res)=>{
              if(res.success){
                that.confirmLoading = false;
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              }else{
                that.confirmLoading = false;
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>